package com.sdtele.tta.operation;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.Task;
import com.sdtele.tta.models.TaskOperation;
import com.sdtele.tta.models.User;
import com.sdtele.tta.utils.TeamUtils;

/**
 * 操作类实现：GetUser(json.operation=getUser)
 * 
 * @author XCream
 */
public class OperationSetTaskImp extends Operation {

	@Override
	public JsonResponse handle(JsonRequest jsonRequest) {
		JsonResponse jres = new JsonResponse();
		User curUser = (User) request.getSession().getAttribute("user");
		long taskId = jsonRequest.getJsonObject().get("taskId").getAsLong();
		Task task = Dac.getInstance().getTaskbyTaskid(taskId);

		if (curUser == null) {
			jres.setStatus(1);
			jres.setMessage("用户未登录");
			return jres;
		}
		if (curUser.getId() != task.getOwnerId()) {
			jres.setStatus(1);
			jres.setMessage("您没有权限修改此任务");
			return jres;
		}
		int action = jsonRequest.getJsonObject().get("action").getAsInt();
		Timestamp updateTime = new Timestamp(System.currentTimeMillis());
		if (action == 1) {
			long userId = jsonRequest.getJsonObject().get("userId").getAsLong();
			TaskOperation op = Dac.getInstance().getOperation(taskId, userId);
			if (op != null) {
				if (op.getOperation() == 0) {
					jres.setStatus(1);
					jres.setMessage("用户为该项目创建人，不允许添加为实施人");
					return jres;
				}
				if (op.getOperation() == 2) {
					jres.setStatus(1);
					jres.setMessage("用户为该项目分阅人，不允许添加为实施人");
					return jres;
				}
			} else {
				TaskOperation taskOp = new TaskOperation();
				taskOp.setTaskId(taskId);
				taskOp.setUserId(userId);
				taskOp.setOperation(1);
				Dac.getInstance().addOperation(taskOp);
				task.setUpdateTime(updateTime);
				System.out.println(task);
				if (taskOp.getId() <= 0) {
					jres.setStatus(1);
					jres.setMessage("添加实施人失败");
					return jres;
				}
				System.out.println(task);
				if (!(Dac.getInstance().chgTask(task))) {
					jres.setStatus(1);
					jres.setMessage("更新任务时间失败");
					return jres;
				}
				User user = Dac.getInstance().getUser(userId);
				jres.setStatus(0);
				jres.setMessage("添加实施人成功");
				jres.setEntity(user);
			}
		}
		if (action == 0) {
			long userId = jsonRequest.getJsonObject().get("userId").getAsLong();
			Task subTask = Dac.getInstance().getTask(userId, taskId);
			if (!(Dac.getInstance().delTaskoperation(taskId, userId))) {
				jres.setStatus(1);
				jres.setMessage("删除实施人失败");
				return jres;
			}
			task.setUpdateTime(updateTime);
			System.out.println(task);
			if (!(Dac.getInstance().chgTask(task))) {
				jres.setStatus(1);
				jres.setMessage("更新任务的更新时间失败");
				return jres;
			}
			if (subTask != null) {
				subTask.setFatherTaskId(0);
				if (!(Dac.getInstance().chgTask(subTask))) {
					jres.setStatus(1);
					jres.setMessage("更新任务的子任务失败");
					return jres;
				}
			}
			User user = Dac.getInstance().getUser(userId);
			jres.setStatus(0);
			jres.setMessage("删除实施人成功");
			jres.setEntity(user);
		}
		return jres;
	}

}
